module("BattleScene", package.seeall)

require("src.Battle.Hero")
require("src.Battle.Corps")
require("src.templates.SkillTemplates")
require("src.templates.MissileTemplates")
require("src.templates.HeroTemplates")
require("src.Battle.Skill")
require("src.Battle.Missile")
require("src.Battle.BattleResult")

function create()
	local scene = cc.Scene:create()

	init(scene)

	return scene
end

function onButtonSkill(self, skillindex)
	local hero = self.corpsLeft.heros[1]
	if (hero) then
		hero:playSkill(skillindex)
	end
end

function onButtonForward( self, eventType )
	for k,hero in ipairs(self.corpsLeft.heros) do
		if (hero) then
			if (eventType == ccui.TouchEventType.ended or eventType == ccui.TouchEventType.canceled) then
				hero:standby()
			elseif (eventType == ccui.TouchEventType.began) then
				hero:moveForward()
			end
		end
	end
end

function onButtonBack( self, eventType )
	--local hero = self.corpsLeft.heros[1]
	for k,hero in ipairs(self.corpsLeft.heros) do
		if (hero) then
			if (eventType == ccui.TouchEventType.ended or eventType == ccui.TouchEventType.canceled) then
				hero:standby()
			elseif (eventType == ccui.TouchEventType.began) then
				hero:moveBack()
			end
		end
	end
end

function init(self)
    local bg = cc.Sprite:create("res/bg.jpg")
    local screenSize = cc.Director:getInstance():getWinSize()

    bg:setPosition(cc.p(screenSize.width/2, screenSize.height/2))
    self:addChild(bg)

	local ui = ccs.GUIReader:getInstance():widgetFromJsonFile("res/UI/BattleUI/BattleUI.ExportJson")
	local uiControl = ccs.GUIReader:getInstance():widgetFromJsonFile("res/UI/BattleUI/ControlUI.ExportJson")
	self.ui = ui

	ui:setSize(screenSize)
	local uiLayer = cc.Layer:create()
	uiLayer:addChild(ui)
	self._labelTitle = ui:getChildByName("Label_Title")
	self._labelRound = ui:getChildByName("Label_Round")

	self._labelTime = ui:getChildByName("Label_Time")
	self._labelItem = ui:getChildByName("Label_Item")

	self._buttonSkills = {}
	for i=1,5 do 
		self._buttonSkills[i] = uiControl:getChildByName("Panel_Skills"):getChildByName("Button_Skill"..tostring(i))
		self._buttonSkills[i]:addTouchEventListener(
			function(sender, eventType)
				if (eventType == ccui.TouchEventType.ended) then
					onButtonSkill(self, i)
				end
			end)
	end

	local buttonClose = ui:getChildByName("Button_Close")
	buttonClose:addTouchEventListener(
		function(sender, eventType)
			if (eventType == ccui.TouchEventType.ended) then
				uiControl:setVisible(not uiControl:isVisible())
			end
		end)
	local buttonCancel = ui:getChildByName("Button_Cancel")
	local buttonSkip = ui:getChildByName("Button_Skip")
	buttonCancel:addTouchEventListener(
		function(sender, eventType)
			if (eventType == ccui.TouchEventType.ended) then
				cc.Director:getInstance():replaceScene(HomeScene:create())
			end
		end)
	buttonSkip:addTouchEventListener(
		function(sender, eventType)
			if (eventType == ccui.TouchEventType.ended) then
				showResultPage(self)
			end
		end)


	self._buttonForward = uiControl:getChildByName("Button_Forward")
	self._buttonBack = uiControl:getChildByName("Button_Back")

	self._buttonForward:addTouchEventListener(
		function ( sender, eventType )
			onButtonForward(self, eventType)
		end)

	self._buttonBack:addTouchEventListener(
		function ( sender, eventType )
			onButtonBack(self, eventType)
		end)

	uiControl:setSize(screenSize)
	uiLayer:addChild(uiControl)
	uiControl:setVisible(fasle)
	
	self:addChild(uiLayer, 2)

	local function onNodeEvent(event)
            if "enter" == event then
                onEnter(self)
            end
        end
     self:registerScriptHandler(onNodeEvent)

     uiControl:setVisible(false)
end

function onEnter(self)
	self.corpsLeft = Corps.create(true, self)

	math.randomseed(os.time())
	heroParams = {
		{
			templateId = 1,
			maxHp = 100
		},
		{
			templateId = 2,
			maxHp = 100
		}
	}

	local num = math.random(2, 5)
	for i=1,num do
		self.corpsLeft:addHero(Hero.create(heroParams[math.random(1,2)], self.corpsLeft))
	end

	self.corpsRight = Corps.create(false, self)
	local num = math.random(2, 5)
	for i=1,num do
		self.corpsRight:addHero(Hero.create(heroParams[math.random(1,2)], self.corpsRight))
	end

	self.corpsLeft:startBattle(self, self.corpsRight)
	self.corpsRight:startBattle(self, self.corpsLeft)
end

function onBattleEnd(self)
	performWithDelay(self,
		function()
			showResultPage(self)
		end, 5)
end

function showResultPage(self)
	self.corpsLeft:clear()
	self.corpsRight:clear()
	self.ui:setVisible(false)
	self:addChild(BattleResult.create(), 10)
end